package com.sg.business.projectgroup.editor;

import org.eclipse.jface.viewers.IInputProvider;
import org.eclipse.jface.viewers.IStructuredSelection;

import com.mobnut.db.model.ModelService;
import com.sg.business.model.ProjectGroup;
import com.sg.business.model.Work;
import com.sg.business.projectgroup.wizard.LaunchWorkWizardForPrjGroup;
import com.sg.widgets.part.CurrentAccountContext;
import com.sg.widgets.viewer.ICreateEditorDelegator;
import com.sg.widgets.viewer.ViewerControl;

public class CreateWorkOfProjectGroup implements ICreateEditorDelegator {

	@Override
	public IInputProvider create(IStructuredSelection selection,
			String editorId, ViewerControl viewerControl) throws Exception {
		Work po = ModelService.createModelObject(Work.class);
		ProjectGroup prjg = (ProjectGroup) viewerControl.getMaster();
		setWorkInfo(po, prjg);
		po.addEventListener(viewerControl);
		// DataObjectWizard w = DataObjectWizard.open(po, editorId, true, null);
		LaunchWorkWizardForPrjGroup open = LaunchWorkWizardForPrjGroup.OPEN(
				null, po, editorId);
		po.removeEventListener(viewerControl);
		return open;
	}

	private void setWorkInfo(Work po, ProjectGroup prjg) {
		CurrentAccountContext currentAccountContext = new CurrentAccountContext();
		String consignerId = currentAccountContext.getAccountInfo()
				.getConsignerId();
		po.setValue(Work.F_CHARGER, consignerId);
		po.setValue(Work.F_PROJECTGROUP_ID, prjg.get_id());
		po.setValue(Work.F_WORK_TYPE, Work.WORK_TYPE_STANDLONE);
	}

}
